﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.business.stamm;
using personalplaner.core.sql;
using personalplaner.business.dienstplan;
using System.Data;
using System.Linq;
using personalplaner.business.extensions;
using personalplaner.business.enums;
using personalplaner.dataprovider.services;

namespace personalplaner.dataprovider.dienstplan
{
	public partial class PlanDataService
	{
		#region Get Methods

		/// <summary>
		/// Returns a list with the active Plannames
		///    - GetActivePlannames(string mandid, bool addEmptyRow)
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="onlyactive"></param>
		/// <returns>Name, PlanID</returns>
		public IEnumerable<ArbeitsPlanHeader> GetArbeitsPlaene(DatabaseSettings settings, bool onlyactive = false)
		{
            var str = string.Format("select planid = PlanID, atid = ATID, name = ap.Name, jahrid = JahrID, schemaid = ap.SchemaID, von = Von, bis = Bis, status = ap.Status, layout = s.Name from ArbeitsPlan ap inner join Schemas s on ap.SchemaId = s.SchemaId");
			if (onlyactive)
                str = string.Format("select planid = PlanID, atid = ATID, name = ap.Name, jahrid = JahrID, schemaid = ap.SchemaID, von = Von, bis = Bis, status = ap.Status, layout = s.Name from ArbeitsPlan ap inner join Schemas s on ap.SchemaId = s.SchemaId where ap.Status = 1");

			var table = SimpleQuerry.ExecuteForDataTable(settings, str);

			try
			{
				return m_ArbeitsPlanData(table);
			}
			catch (Exception e)
			{
				throw new Exception("GetArbeitsPlaene caused an error", e);
			}
		}

		/// <summary>
		/// Returns the specified arbeitsplan
		///    - GetActivePlannames(string mandid, bool addEmptyRow)
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="planid"></param>
		/// <returns></returns>
		public ArbeitsPlanHeader GetArbeitsPlan(DatabaseSettings settings, int planid)
		{
			//var str = string.Format("select planid = PlanID, atid = ATID, name = Name, jahrid = JahrID, schemaid = SchemaID, von = Von, bis = Bis, status = Status from ArbeitsPlan where PlanID = {0}", planid);
            var str = string.Format("select planid = PlanID, atid = ATID, name = ap.Name, jahrid = JahrID, schemaid = ap.SchemaID, von = Von, bis = Bis, status = ap.Status, layout = s.Name from ArbeitsPlan ap inner join Schemas s on ap.SchemaId = s.SchemaId where PlanID = {0}", planid);

			var table = SimpleQuerry.ExecuteForDataTable(settings, str);

			if (table.Rows.Count <= 0)
				return null;

			try
			{
				return m_ArbeitsPlanData(table).FirstOrDefault();
			}
			catch (Exception e)
			{
				throw new Exception("GetArbeitsPlan caused an error", e);
			}
		}

        /// <summary>
        /// Creates a list of every person in a plan. 
		///		- copy of  public static DataView GetPersonenInPlan(int planID)
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="planID">PlanID Attribut</param>
        /// <returns>prsID,name,nachname,abteilungid,abteilungname,prsschemaid,von,bis,rgb,pensum</returns>
        public IEnumerable<DienstPlanRowHeader> GetPlanRowHeaders(DatabaseSettings settings, int planID)
        {
            var query = new Querry(settings, "get_personeninplan");
            query.AddParameter("@planid", planID);

            var table = query.ExecuteForDataTable();

            try
            {
                return m_DienstPlanRowHeaderData(table);
            }
            catch (Exception e)
            {
                throw new Exception("GetPlanRowHeaders caused an error", e);
            }
        }

        /// <summary>
        /// Creates a DataView with specific Data of a row. datum,name,dienstid,stunden,rgb
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="planID">PlanID</param>
        /// <param name="prsSchemaID">PrsSchemaID</param>
        /// <param name="prsid"></param>
        /// <returns>datum,name,dienstid,stunden,rgb</returns>
        public IEnumerable<PlanItem> GetPlanRow(DatabaseSettings settings, int planID, int prsSchemaID, int prsid)
        {
            var query = new Querry(settings, "get_planrow");
            query.AddParameter("@panid", planID);
            query.AddParameter("@prsschemaid", prsSchemaID);
            query.AddParameter("@prsid", prsid);
            var table = query.ExecuteForDataTable();

            try
            {
                return m_DienstPlanRowData(table);
            }
            catch (Exception e)
            {
                throw new Exception("GetPlanRow caused an error", e);
            }
        }

		public string GetPlanName(DatabaseSettings settings, int planId)
		{
			var dv = SimpleQuerry.ExecuteForDataTable(settings, string.Format("select name = Name from ArbeitsPlan where PlanID = {0}", planId));

			if (dv.Rows.Count <= 0 || !dv.Columns.Contains("name"))
				return "";

			return dv.Rows[0].Field<string>("name");
		}

		/// <summary>
		/// [vortrag,soll,haben,ferien,anpassung]
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="prsid"></param>
		/// <param name="abteilungid"></param>
		/// <param name="planid"></param>
		/// <returns></returns>
		public TimeSaldo GetSaldo(DatabaseSettings settings, int prsid, int abteilungid, int planid)
		{
			var query = new Querry(settings, "get_saldo");
			query.AddParameter("@prsid", prsid);
			query.AddParameter("@planid", planid);
			query.AddParameter("@abteilungid", abteilungid);

			var table = query.ExecuteForDataTable();

			try
			{
				return m_TimeSaldoData(table);
			}
			catch (Exception e)
			{
				throw new Exception("GetSaldo caused an error", e);
			}
		}

		public int GetSchemaId(DatabaseSettings settings, int planid)
		{
			var dv = SimpleQuerry.ExecuteForDataTable(settings, string.Format("select schemaid = SchemaID from ArbeitsPlan where PlanID = {0}", planid));
			if (dv.Rows.Count <= 0 || !dv.Columns.Contains("schemaid"))
				return 0;

			return dv.Rows[0].Field<int>("schemaid");
		}

		public int GetPlanId(DatabaseSettings settings, int schemaid)
		{
			var dv = SimpleQuerry.ExecuteForDataTable(settings, string.Format("select planid = PlanID from ArbeitsPlan where SchemaID = {0}", schemaid));
			if (dv.Rows.Count <= 0 || !dv.Columns.Contains("planid"))
				return 0;

			return dv.Rows[0].Field<int>("planid");
		}

        /// <summary>
        /// [von]
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="planID">planid</param>
        /// <returns>[von]</returns>
        public IEnumerable<Feiertag> GetFeiertageInPlan(DatabaseSettings settings, int planID)
        {
            var table = SimpleQuerry.ExecuteForDataTable(settings, string.Format("select von = Von, bis = Bis, jahrid = JahrID from ArbeitsPlan where PlanID = {0}", planID));

            if (table.Rows.Count <= 0)
                return null;

            try
            {
				var ft = CommonDataService.m_FeiretageData(table);

				if (ft == null || ft.Count() <= 0)
					return null;

                //string ex = string.Format("select Von from ArbeitsTage where typ = 2 and Von >= '{0}" + dv[0].Row[0].ToString() + "' and Von <= '" + dv[0].Row[1].ToString() + "' and JahrID = " + dv[0].Row[2].ToString() + " order by Von";

				var ex = string.Format("select name = Name, von = Von, bis = Bis, jahrid = JahrID, atid = ATID, tage = Tage from ArbeitsTage where typ = 2 and Von >= '{0}' and Von <= '{1}' and JahrID = {2} order by Von", ft.First().Von.ToString("yyyyMMdd"), ft.First().Bis.ToString("yyyyMMdd"), ft.First().JahrId);
				//string ex = string.Format("select Von from ArbeitsTage where typ = 2 and Von >= '{0}' and Von <= '{1}' and JahrID = {2} order by Von", ft.First().Von.ToString("yyyyMMdd"), ft.First().Bis.ToString("yyyyMMdd"), ft.First().JahrId);
                var table2 = SimpleQuerry.ExecuteForDataTable(settings, ex);

                if (table2.Rows.Count <= 0)
                    return null;

				return CommonDataService.m_FeiretageData(table2);
            }
            catch (Exception e)
            {
                throw new Exception("GetFeiertageInPlan caused an error", e);
            }
        }

        public string GetSchemaName(DatabaseSettings settings, int schemaid)
        {
            var dv = SimpleQuerry.ExecuteForDataTable(settings, string.Format("select name = Name from Schemas where SchemaID = {0}", schemaid));
            if (dv.Rows.Count <= 0)
                return string.Empty;

            return dv.Columns.Contains("name") ? dv.Rows[0].Field<string>("name") : string.Empty;
        }

		/// <summary>
		/// Checks if one of the abteilungen form the schema is already contained in a plan at the given date (month and year)
		///		- static public bool CheckIfPlanExists(int monatID, int schemaID, int jahrID)
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="atId"></param>
		/// <param name="schemaId"></param>
		/// <param name="jahrId"></param>
		/// <returns></returns>
		public bool DoesPlanExist(DatabaseSettings settings, int atId, int schemaId, int jahrId)
		{
			var stp = new Querry(settings, "chk_abteilungverplant");
			stp.AddParameter("@monatID", atId);
			stp.AddParameter("@schemaID", schemaId);
			stp.AddParameter("@jahrID", jahrId);
			var dv = stp.ExecuteForDataTable();

			if (dv.Rows.Count > 0)
			{
				var amountExists = dv.Columns.Contains("amount");
				try
				{
					return amountExists ? (dv.Rows[0].Field<int?>("amount") ?? 0) > 0 : false;
				}
				catch
				{
				}
			}

			return false;
		}

		/// <summary>
		/// Creates a new ArbeitsPlan [planid]
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="planname">planname</param>
		/// <param name="arbeitstageid">atid</param>
		/// <param name="schemaid">schemaid</param>
		/// <param name="continuous">Soll das Schema fortlaufend erstellt werden?</param>
		/// <returns>[planid]</returns>
		public int CreateNewPlan(DatabaseSettings settings, string planname, int arbeitstageid, int schemaid, bool continuous)
		{
			var query = new Querry(settings);

			if (!continuous)
				query.SetSTPName("set_neuplan");
			else
				query.SetSTPName("set_newshemaplan");

			query.AddParameter("@planname", planname);
			query.AddParameter("@atid", arbeitstageid);
			query.AddParameter("@schemaid", schemaid);

			var dv = query.ExecuteForDataTable(true);

			if (dv.Rows.Count > 0 && dv.Columns.Contains("planid"))
			{
				try
				{
					return dv.Rows[0].Field<int>("planid");
				}
				catch (InvalidCastException e)
				{
					personalplaner.core.errorhandling.Logger.ToXML(
						string.Format("Create plan returned no planid; Option:ContinuousSchema = {0}", continuous.ToString()),
						"PlanData.CreateNewPlan(string planname, int arbeitstageid, int schemaid, bool continuous)", e.Message);

					personalplaner.core.errorhandling.ActionLogger.LoggMessage(core.errorhandling.MessageType.SQL_ERROR,
					                                               string.Format("Create plan returned no planid; Option:ContinuousSchema = ",continuous.ToString()));
				}
			}

			return 0;
		}

		/// <summary>
		/// Gets a list of <see cref="SaldoAnpassung"/> items for a certain person
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="prsid">prsid</param>
		/// <param name="abteilungid">abteilungid</param>
		/// <param name="planid">planid</param>
		/// <returns>[saldoid,datum,grund,stunden]</returns>
		public IEnumerable<SaldoAnpassung> GetSaldoAnpassungen(DatabaseSettings settings, int prsid, int abteilungid, int planid)
		{
			var query = new Querry(settings, "get_saldoanpassung");
			query.AddParameter("@prsid", prsid);
			query.AddParameter("@abteilungid", abteilungid);
			query.AddParameter("@planid", planid);

			var table = query.ExecuteForDataTable();

			if (table.Rows.Count <= 0)
				return null;

			try
			{
				return m_SaldoEditData(table);
			}
			catch (Exception e)
			{
				throw new Exception("GetSaldoAnpassung caused an error", e);
			}
		}

        public int GetDienstCountInPlanPerDay(DatabaseSettings settings, int dienstid, int planid, int abteilungid, DateTime date)
        {
            var str = string.Format("select count = COUNT(*) from Dienstplan where PlanID = {0} and DienstID = {1} and AbteilungID = {2} and Datum = CONVERT(datetime,'{3}',112)", planid, dienstid, abteilungid, date.ToString("yyyyMMdd"));
            var dv = SimpleQuerry.ExecuteForDataTable(settings, str);

            if (dv.Rows.Count <= 0 || !dv.Columns.Contains("count"))
                return 0;

            return dv.Rows[0].Field<int>("count");
        }

        public int GetDienstCountInPlanPerDay(DatabaseSettings settings, int planid, int abteilungid, DateTime date, int prsid)
        {
            var str = string.Format("select COUNT(*) from Dienstplan where PlanID = {0} and AbteilungID = {1} and Datum = CONVERT(datetime,'{2}',112) and PrsID = {3}", planid, abteilungid, date.ToString("yyyyMMdd"), prsid);
            var dv = SimpleQuerry.ExecuteForDataTable(settings, str);

            if (dv.Rows.Count <= 0 || !dv.Columns.Contains("count"))
                return 0;

            return dv.Rows[0].Field<int>("count");
        }

        public IEnumerable<int> GetPersonenWithoutDienst(DatabaseSettings settings,  int planid, int abteilungid, DateTime date)
        {
            var query = new Querry(settings, "get_personenwithoutdienst");
            query.AddParameter("@planid", planid);
            query.AddParameter("@abteilungid", abteilungid);
            query.AddParameter("@date", date.ToString("yyyyMMdd"));

            var table = query.ExecuteForDataTable();

            if (table.Rows.Count <= 0)
				return null;

			try
			{
                return m_PersonenWithoutDienstData(table);
			}
			catch (Exception e)
			{
                throw new Exception("GetPersonenWithoutDienst caused an error", e);
			}
        }

        /// <summary>
        /// Gets a list of items containing information about each plan in the year
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="prsid"></param>
        /// <param name="yearid"></param>
        /// <returns></returns>
        public IEnumerable<PlanOverviewHeader> GetPlanOverviewHeaders(DatabaseSettings settings, int prsid, int yearid)
        {
            var stp = new Querry(settings, "get_personplanovervieweaders");
            stp.AddParameter("@prsid", prsid);
            stp.AddParameter("@jahrid", yearid);

            var table =  stp.ExecuteForDataTable();

            if (table.Rows.Count <= 0)
                return null;

            try
            {
                return m_PlanOverviewHeadersData(table);
            }
            catch (Exception e)
            {
                throw new Exception("GetPlanOverviewHeaders caused an error", e);
            }
        }

		///// <summary>
		///// gets a list of items containing information about each planed item in a plan
		///// </summary>
		///// <param name="settings"></param>
		///// <param name="prsid"></param>
		///// <param name="abteilungid"></param>
		///// <param name="planid"></param>
		///// <param name="dienstids"></param>
		///// <param name="dates"></param>
		///// <param name="all"></param>
		///// <returns></returns>
		//public IEnumerable<PlanOverviewItem> GetPlanOverviewRow(DatabaseSettings settings, int prsid, int abteilungid, int planid, int[] dienstids, DateTime[] dates, bool all)
		//{
		//    var querry = new Querry(settings, "get_personjpcolumn");
		//    querry.AddParameter("@prsid", prsid);
		//    querry.AddParameter("@abteilungid", abteilungid);
		//    querry.AddParameter("@planid", planid);
		//    querry.AddParameter("@dienstids", DienstIDs);
		//    querry.AddParameter("@datumer", datumer);
		//    querry.AddParameter("@selectall", all?1:0);

		//    var table = querry.ExecuteForDataTable();

		//    if (table.Rows.Count <= 0)
		//        return null;

		//    try
		//    {
		//        return m_OverviewRowData(table);
		//    }
		//    catch (Exception e)
		//    {
		//        throw new Exception("GetPlanOverviewRow caused an error", e);
		//    }
		//}

		/// <summary>
		/// Creates a list of shortdienst items containig all holidays in a year
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="prsid"></param>
		/// <param name="atid"></param>
		/// <param name="yearid"></param>
		/// <returns></returns>
		public IEnumerable<ShortDienst> GetPlanHolidays(DatabaseSettings settings, int prsid, int atid, int yearid)
        {
            var querry = new Querry(settings, "get_personferiencolumndienst");
            querry.AddParameter("@prsid", prsid);
            querry.AddParameter("@atid", atid);
            querry.AddParameter("@jahrid", yearid);

            var table =  querry.ExecuteForDataTable();

            if (table.Rows.Count <= 0)
                return null;

            try
            {
                return m_ShortDienstData(table);
            }
            catch (Exception e)
            {
                throw new Exception("GetPlanOverviewHeaders caused an error", e);
            }
        }

        /// <summary>
        /// gets a list of <see cref="ArbeitsPlanHeader"/> that contain the schema
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="schemaId"></param>
        /// <returns></returns>
        public IEnumerable<ArbeitsPlanHeader> GetPlaeneFromSchemaId(DatabaseSettings settings, int schemaId)
        {
            var str = string.Format("select planid = PlanID, atid = ATID, name = Name, jahrid = JahrID, schemaid = SchemaID, von = Von, bis = Bis, status = Status from ArbeitsPlan where SchemaID = {0}", schemaId);
            var table = SimpleQuerry.ExecuteForDataTable(settings, str);

            try
            {
                return m_ArbeitsPlanData(table);
            }
            catch (Exception e)
            {
                throw new Exception("GetPlaeneFromSchemaId caused an error", e);
            }
        }

		#endregion

        #region Set Methods

        /// <summary>
        /// Saves a dienst in a specific plan on a specific day
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        /// <param name="date"></param>
        /// <param name="dienstId"></param>
        public bool SaveDienstInPlan(DatabaseSettings settings, int planId, int prsId, int abteilungId, DateTime date, int dienstId)
        {
            var query = new Querry(settings, "set_updsetdienst");

            query.AddParameter("@planid", planId);
            query.AddParameter("@prsid", prsId);
            query.AddParameter("@abteilungid", abteilungId);
            query.AddParameter("@datum", date);
            query.AddParameter("@dienstid", dienstId);

            return query.Execute(false);
        }

        /// <summary>
        /// recalculates the saldos in a plan
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        public bool RecalculateSaldosInPlan(DatabaseSettings settings, int planId, int prsId, int abteilungId)
        {
            var query = new Querry(settings, "set_recalculateplanzeit");

            query.AddParameter("@planid", planId);
            query.AddParameter("@prsid", prsId);
            query.AddParameter("@abteilungid", abteilungId);

            return query.Execute(false);
        }

        /// <summary>
        /// Saves a color in a specific plan on a specific day
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        /// <param name="date"></param>
        /// <param name="argb"></param>
        public bool SaveColorInPlan(DatabaseSettings settings, int planId, int prsId, int abteilungId, DateTime date, int argb)
        {
            var query = new Querry(settings, "set_updsetcolor");
            
            query.AddParameter("@planid", planId);
            query.AddParameter("@prsid", prsId);
            query.AddParameter("@abteilungid", abteilungId);
            query.AddParameter("@datum", date);
            query.AddParameter("@argb", argb);
            
            return query.Execute(false);
        }

		/// <summary>
		/// Initializes all Dienstserien in Database for a plan
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="planId">planid</param>
		public void InitializeDienstSerie(DatabaseSettings settings, int planId)
		{
			var query = new Querry(settings, "get_plandienstserie");
			query.AddParameter("@planid", planId);
			var dv = query.ExecuteForDataTable();

			foreach (DataRow row in dv.Rows)
			{
				var prsid = row.Field<int>("prsid");
				var abteilungid = row.Field<int>("abteilungid");
				var dienstid = row.Field<int>("dienstid");
				var von = row.Field<DateTime>("von");
				var bis = row.Field<DateTime>("bis");
				var typ = row.Field<string>("typ");
				var tag = row.Field<int>("tag");
				var planvon = row.Field<DateTime>("planvon");
				var planbis = row.Field<DateTime>("planbis");

				switch (typ)
				{
					//Wöchentlich
					case "W":
						DayOfWeek dow = GetDayOfWeek(tag);
						var tage = planbis.ToInt() - planvon.ToInt();

						for (int c = 0; c < tage; c++)
						{
							var dt = new DateTime(planvon.Year, planvon.Month, c + 1);
							if (dt.DayOfWeek == dow)
							{
								if (von.ToInt() <= dt.ToInt() && bis.ToInt() >= dt.ToInt())
									SaveDienstInPlan(settings, planId, prsid, abteilungid, dt, dienstid);
							}
						}

						break;

					//Monatlich
					case "M":
						var dte = new DateTime(planvon.Year, planvon.Month, tag);

						if (von.ToInt() <= dte.ToInt() && bis.ToInt() >= dte.ToInt())
							SaveDienstInPlan(settings, planId, prsid, abteilungid, dte, dienstid);
						break;
				}
			}
		}

		private static DayOfWeek GetDayOfWeek(int day)
		{
			DayOfWeek tmpDay;

			switch (day)
			{
				case 1:
					tmpDay = DayOfWeek.Monday;
					break;
				case 2:
					tmpDay = DayOfWeek.Tuesday;
					break;
				case 3:
					tmpDay = DayOfWeek.Wednesday;
					break;
				case 4:
					tmpDay = DayOfWeek.Thursday;
					break;
				case 5:
					tmpDay = DayOfWeek.Friday;
					break;
				case 6:
					tmpDay = DayOfWeek.Saturday;
					break;
				case 7:
					tmpDay = DayOfWeek.Sunday;
					break;
				default:
					tmpDay = DayOfWeek.Monday;
					break;
			}
			return tmpDay;
		}

		/// <summary>
		/// sets a new saldoanpassung to a person and returns the saldo id
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="anpassung"></param>
		/// <returns></returns>
		public int SetSaldoAnpassung(DatabaseSettings settings, SaldoAnpassung anpassung)
		{
			var query = new Querry(settings, "set_saldoanpassung");

			query.AddParameter("@PlanID", anpassung.PlanId);
			query.AddParameter("@abteilungid", anpassung.AbteilungId);
			query.AddParameter("@PrsID", anpassung.PrsId);
			query.AddParameter("@Datum", anpassung.Datum);
			query.AddParameter("@Grund", anpassung.Reason);
			query.AddParameter("@Stunden", anpassung.Amount);
			var retval = query.AddParameter("@retval", 0, ParameterDirection.Output);

			query.Execute(true);

			return (int)retval.Value;
		}

        #endregion

		#region Update Methods

		/// <summary>
		/// Updates the Status of a plan
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="planId">PlanID</param>
		/// <param name="status">Status</param>
		public bool UpdatePlanStatus(DatabaseSettings settings, int planId, enStatus status)
		{
			int st = 1;
			switch (status)
			{
				case enStatus.Active:
					st = 1;
					break;
				case enStatus.Inactive:
					st = 2;
					break;
			}
			var query = new Querry(settings, "upd_planstatus");

			query.AddParameter("@statusin", st);
			query.AddParameter("@planidin", planId);

			return query.Execute(true);
		}

		/// <summary>
		/// Updates the name of the plan
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="planId"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public bool UpdatePlanName(DatabaseSettings settings, int planId, string name)
		{
			var str = string.Format("update ArbeitsPlan set Name = {0} where PlanID = {1}", name, planId);
			return SimpleQuerry.Execute(settings, str);
		}

		#endregion

		#region Delete Methods

		/// <summary>
		/// Deletes the plan
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="planId"></param>
		/// <returns></returns>
		public bool DeletePlan(DatabaseSettings settings, int planId)
		{
			var query = new Querry(settings, "del_plan");

			query.AddParameter("@planid", planId);

			return query.Execute(true);
		}

		public bool DeleteSaldoAnpassung(DatabaseSettings settings, SaldoAnpassung anpassung)
		{
			return DeleteSaldoAnpassung(settings, anpassung.SaldoId);
		}

		public bool DeleteSaldoAnpassung(DatabaseSettings settings, int saldoid)
		{
			var str = string.Format("delete from SaldoAnpassung where SaldoID = {0}", saldoid);
			return SimpleQuerry.Execute(settings, str);
		}

		#endregion
    }
}
